package regominer2;

import java.util.LinkedList;
import java.util.Random;

import javax.swing.JButton;
import regominer2.Hexagon;

import regominer2.RegomineR2;

class Board
{
	private LinkedList<Hexagon> tiles;
	public static int size = 21;
	private int minesCount;
	private RegomineR2 parent;
	
	
	public Board(int size, RegomineR2 parent)
	{
		tiles = new LinkedList();
		this.size = size;
		this.parent = parent;
		minesCount = size * size / 10; 
		generateMines(2, 2, minesCount);
		generateNotmines();
		generateNeighborhoods();
	//	printTiles();
	}
	
	private void generateMines(int row, int number, int howMany)
	{
		Random r = new Random();
		while(howMany-- > 0)
		{
			while(true)
			{
				boolean occupied = false;
				int newrow = r.nextInt(size);
				int newnumber = r.nextInt((newrow % 2 == 0 ? size - 1 : size));
				for(Hexagon h : tiles)
				{
					if((h.getRow() == newrow && h.getNumber() == newnumber)||(newrow == row && newnumber == number)){
						occupied = true;
						break;
					}
				}
				if(occupied)
					continue;
				tiles.add(new Mine(newrow, newnumber, parent.getButton(newrow, newnumber)));
				break;
			}
		}
	}
	
	public Hexagon findTileByPos(int x, int y)
	{
		int row = (y-RegomineR2.margin) / RegomineR2.tileSize;
		int number = ((x - RegomineR2.margin - (row % 2 == 0 ? RegomineR2.tileSize / 2 : 0)) / RegomineR2.tileSize);
		return findTile(row, number);
	}
	
	public Hexagon findTile(int row, int number)
	{
		for(Hexagon h : tiles)
		{
			if(h.getRow() == row && h.getNumber() == number){
				return h;
			}
		}
		return null;
	}
	
	private boolean fwain(int row, int number, boolean oldResult)
	{
		if(findTile(row, number)==null)
		{
			Notmine n = new Notmine(row, number, parent.getButton(row, number));
			tiles.add(n);
			return recursiveGenerateNotmines(n);
		}
		return oldResult;
	}
	
	
	private boolean recursiveGenerateNotmines(Hexagon h)
	{
		
		int row = h.getRow();
		int number = h.getNumber();
		boolean result = false;
		if(row % 2 == 0)
		{
			if(number > 0)
			{
				result = fwain(row, number - 1, result);
			}
			if(number < size - 2)
			{
				result = fwain(row, number + 1, result);
			}
			if(row > 0)
			{
				result = fwain(row - 1, number, result);
				result = fwain(row - 1, number + 1, result);
			}
			if(row < size - 1)
			{
				result = fwain(row + 1, number, result);
				result = fwain(row + 1, number + 1, result);
			}
		}
		else 
		{				
			if(number > 0)
			{
				result = fwain(row, number - 1, result);
				result = fwain(row - 1, number - 1, result);
				result = fwain(row + 1, number - 1, result);
			}
			if(number < size - 1)
			{
				result = fwain(row, number + 1, result);
				result = fwain(row - 1, number, result);
				result = fwain(row + 1, number, result);
			}
		}
		return result;
	}
	
	private void generateNotmines()
	{
		Hexagon h = tiles.getFirst();
		recursiveGenerateNotmines(h);
	}
	
	private void generateNeighborhoods()
	{
		for(Hexagon h : tiles)
		{
			int row = h.getRow();
			int offset = (row % 2 == 1 ? -1 : 1);
			int number = h.getNumber();
			h.addNeighbor(findTile(row - 1, number + offset));
			h.addNeighbor(findTile(row - 1, number));
			h.addNeighbor(findTile(row, number + 1));
			h.addNeighbor(findTile(row, number - 1));
			h.addNeighbor(findTile(row + 1, number));
			h.addNeighbor(findTile(row + 1, number + offset));
		}
	}
	
	private void printTiles()
	{
		System.out.println(size);
		for(Hexagon h : tiles)
		{
			if(h.getClass() == Mine.class)
			{
				System.out.print("\nMina\t");
			}
			else
			{
				System.out.print("\nNieMina\t");
			}
			
			System.out.print(h.getRow() + "\t" + h.getNumber() + "\t" + (char)(h.getRow()+'a') + (char)(h.getNumber()+'a') + "\t" + h.getValue());
		}
	}

	public void Lclick(Hexagon tile)
	{
		if(!tile.isDead())
		{
			myButton b = tile.getButton();
	    	int value = tile.getValue();
	    	
			tile.setDead(true);
			b.setSelected(true);
	    	if(value == 9)
	    		b.setText("X");
	    	else if(value == 0)
	    	{
	    		for(Hexagon h : tile.getNeighbors())
	    		{
	    			Lclick(h);
	    		}
	    	}
	    	else
	    		b.setText(String.valueOf(value));
		}
		
	}
	
	public void Rclick(Hexagon tile)
	{
		myButton b = tile.getButton();
		if(b.getText()=="F"){
			b.setText(" ");
			tile.setDead(false);
		}
		else
		{
			b.setText("F");
			tile.setDead(true);
		}
		
	}
	
	public void Bclick(Hexagon tile)
	{
		
	}
}